package sis.security;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.List;

import sis.studentinfo.Account;
import sis.studentinfo.Permissionexception;

public class SecureProxy implements InvocationHandler {

	private List<String> secureMethods;
	private Account target;

	public SecureProxy(Account account, String... secureMethods) {
		this.target = account;
		this.secureMethods = Arrays.asList(secureMethods);
	}

	public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
		try {
			if (isSecure(method)) {
				throw new Permissionexception();
			}
				return method.invoke(target, args);
		} catch (InvocationTargetException e) {
			throw e.getTargetException();
		}
	}

	private boolean isSecure(Method method) {
		return secureMethods.contains(method.getName());
	}

}
